package com.fingard.dsp.bank.directbank.mybank01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISendSmsCode;
import com.fingard.dsp.bank.reqrespobj.sendsmscode.ReqCheckSmsCode;
import com.fingard.dsp.bank.reqrespobj.sendsmscode.ReqSendSmsCode;
import com.fingard.dsp.bank.reqrespobj.sendsmscode.RetAtsCheckSmsCode;
import com.fingard.dsp.bank.reqrespobj.sendsmscode.RetAtsSendSmsCode;
import com.fingard.dsp.bank.reqrespobj.sendsmscode.detail.RetAtsSmsCodeDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by Ross on 2017-06-13.
 */
public class MYBank01SendSmsCode extends MYBank01Base implements ISendSmsCode {

    public RetAtsSendSmsCode sendSmsCode(ReqSendSmsCode p_req) throws Exception {
        RetAtsSendSmsCode retAts = new RetAtsSendSmsCode(p_req.reqHeader);
        RetAtsSmsCodeDetail tmpRetDetail = new RetAtsSmsCodeDetail();
        try {
            //赋值通用参数
            MYBank01Header bankHeader = new MYBank01Header();
            bankHeader.function = "ant.ebank.auth.smscode.send";
            bankHeader.signFields = "bizNo,bizName,certNo";
            bankHeader.signValueField = "sign";

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            Date tmpDateNow = new Date();
            bankHeader.reqMsgID = Format.DateTime17Format.format(tmpDateNow);
            bankHeader.reqTime = tmpDateNow;

            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("body");
            xmlWriter.writeElementString("partner", bankFront.operator);
            xmlWriter.writeElementString("bizNo", p_req.singleDetail.reqSeqID);
            String bizName = "P_CROSS_TRANSFER";
            if (p_req.singleDetail.bizName.equals("1")) {
                bizName = "P_INNER_TRANSFER";
            } else if (p_req.singleDetail.bizName.equals("2")) {
                bizName = "P_CROSS_TRANSFER";
            } else if (p_req.singleDetail.bizName.equals("3")) {
                bizName = "P_BATCH_TRANSFER";
            }
            xmlWriter.writeElementString("bizName", bizName);
            String certNo = "";
            if (p_req.singleDetail.certType.equals("0")) {
                certNo = p_req.singleDetail.certNum;
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "当前证件类型网商银行暂不支持";
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }

            xmlWriter.writeElementString("certNo", certNo);
            xmlWriter.writeElementString("sign", "");
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultStatus");
                    String tmpRetInfo = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultMsg");
                    if (tmpRetCode != null && tmpRetCode.equals("S")) {
                        if (tmpBkReader.read("/document/response/body")) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.cellPhone = tmpBkReader.readedXElement.getSingleText("operatorTelNo");
                            tmpRetDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("bizNo");
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsCheckSmsCode checkSmsCode(ReqCheckSmsCode p_req) throws Exception {
        return null;
    }
}
